<script setup lang="ts">
import TeamCardList from "@/components/TeamCardList.vue";
import { onMounted, ref } from "vue";
import { queryCreateTeam } from "@/services/teamService";
import { showToast } from "vant";
import { getCurrentLoginUserState } from "@/state/userState";

//队伍列表
let teamList = ref([]);
onMounted(async () => {
  //获取当前登录用户
  const user = await getCurrentLoginUserState();
  //请求队伍数据
  const res = await queryCreateTeam({ userId: user.id });
  const { message, description, code, data } = res;
  if (code === 0) {
    showToast("请求成功");
    teamList.value = data;
  } else {
    showToast(description.length ? description : message);
  }
});
//搜索关键词
let keyWord = ref("");
//搜索
const onSearch = async (keyWord) => {
  //请求队伍数据
  const res = await queryCreateTeam({ searchText: keyWord });
  const { message, description, code, data } = res;
  if (code === 0) {
    showToast("搜索成功");
    teamList.value = data;
  } else {
    showToast(description.length ? description : message);
  }
};
</script>

<template>
  <van-search
    v-model="keyWord"
    placeholder="请输入搜索关键词"
    @search="onSearch"
  />
  <team-card-list :team-list="teamList"></team-card-list>
</template>

<style scoped></style>
